# Contributor: ScrumpyJack <scrumpyjack@st.ilet.to>
# Maintainer: Celeste <cielesti@protonmail.com>
pkgname=w3m
_pkgver=0.5.3+git20230718
pkgver=${_pkgver/+git/.}
_gitrev=ee66aabc3987000c2851bce6ade4dcbb0b037d81
pkgrel=1
pkgdesc="text-based web & gopher browser, as well as pager"
url="https://github.com/tats/w3m"
license="MIT"
arch="all"
makedepends="
	gc-dev
	imlib2-dev
	linux-headers
	ncurses-dev
	openssl-dev>3
	"
subpackages="
	$pkgname-doc
	$pkgname-perl::noarch
	$pkgname-image
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/tats/w3m/archive/$_gitrev.tar.gz"
builddir="$srcdir/$pkgname-$_gitrev"

# secfixes:
#   0.5.3.20230718-r0:
#     - CVE-2023-38252
#     - CVE-2023-38253
#   0.5.3.20230121-r0:
#     - CVE-2022-38223
#   0.5.3.20180125-r0:
#     - CVE-2018-6196
#     - CVE-2018-6197
#     - CVE-2018-6198

build() {
	PERL=/usr/bin/perl ./configure \
		--prefix=/usr \
		--libexecdir=/usr/lib \
		--enable-image=fb \
		--with-ssl \
		--disable-nntp \
		--disable-dict \
		--disable-help-cgi \
		--with-imagelib=imlib2 \
		--with-termlib=ncurses \
		--with-editor="/usr/bin/vi" \
		--with-browser="/usr/bin/xdg-open" \
		--disable-w3mmailer
	make
}

check() {
	cd tests
	sh run_tests
}

package() {
	make DESTDIR="$pkgdir" install
	# when ./configure has --disable-help-cgi passed, the (H)elp key
	# is hardcoded to open "w3mhelp-w3m_en.html", instead of the
	# w3mhelp.cgi script. 'make install' has install-scripts as a
	# target, but not install-helpfile, probably ./configure doesn't
	# get tested with --disable-help-cgi very often. anyway, we have
	# to make install-helpfile or else the (H)elp key won't work.
	# additionally, the _en & _ja help files have hyperlinks to each
	# other, so install both, unless we want to patch the help file
	make DESTDIR="$pkgdir" \
		HELP_ALLFILES="w3mhelp-w3m_en.html w3mhelp-w3m_ja.html" \
		install-helpfile

	# the following 3 Perl scripts have dependencies not found in Alpine
	# xface2xpm needs uncompface, w3mdict.cgi needs dictionary client
	# multipart.cgi needs NKF (Network Kanji code conversion Filter)
	rm "$pkgdir"/usr/lib/w3m/xface2xpm
	rm "$pkgdir"/usr/lib/w3m/cgi-bin/w3mdict.cgi
	rm "$pkgdir"/usr/lib/w3m/cgi-bin/multipart.cgi
	# w3mmail.cgi needs cmd:sendmail (which Alpine has pkgs for), but
	# removing it because --disable-w3mmailer causes w3m to use an
	# external program (default: /usr/bin/mail) to handle mailto: links
	rm "$pkgdir"/usr/lib/w3m/cgi-bin/w3mmail.cgi

	install -Dm644 doc/* -t "$pkgdir"/usr/share/doc/$pkgname
	install -d "$pkgdir"/usr/share/licenses/$pkgname
	ln -s /usr/share/doc/$pkgname/README \
		"$pkgdir"/usr/share/licenses/$pkgname
}

perl() {
	pkgdesc="$pkgdesc (perl helper scripts)"
	depends="perl cmd:man"			# w3mman needs cmd:man
	amove usr/bin/w3mman
	amove usr/lib/w3m/cgi-bin/*.cgi
	amove usr/share/w3m/*.pl
}

image() {
	pkgdesc="$pkgdesc (inline image support)"
	install_if="$pkgname=$pkgver-r$pkgrel imlib2"
	amove usr/lib/w3m/w3mimgdisplay
}

sha512sums="
cb023fe0b7ac4f009b7ecdee0134d4bf727b9f58e61b23b832979c27d90dbf7104bafd13d2aea5ecd1190262b103bcc30a1945a7845beb6aeee48751666778d3  w3m-0.5.3.20230718.tar.gz
"
